package cn.zhage.aimin.canal.client.autoconfigure;

import cn.zhage.aimin.canal.client.EntityHandlerHolder;
import cn.zhage.aimin.canal.client.client.CanalClient;
import cn.zhage.aimin.canal.client.client.ClusterCanalClient;
import cn.zhage.aimin.canal.client.config.CanalProperties;
import cn.zhage.aimin.canal.client.handler.EntityHandler;
import cn.zhage.aimin.canal.client.handler.MessageHandler;
import cn.zhage.aimin.canal.client.handler.RowDataHandler;
import cn.zhage.aimin.canal.client.handler.impl.MessageHandlerImpl;
import cn.zhage.aimin.canal.client.handler.impl.RowDataHandlerImpl;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.List;

/*
 *
 * @author 渣哥
 */
@AutoConfiguration
@EnableConfigurationProperties(CanalProperties.class)
@ConditionalOnBean(value = {EntityHandler.class})
@ConditionalOnProperty(value = "canal.mode", havingValue = "cluster" )
public class ClusterCanalClientAutoConfiguration {

    @Bean
    public CanalClient clusterCanalClient(CanalProperties canalProperties, MessageHandler<Message> messageHandler) {
        return new ClusterCanalClient(canalProperties, messageHandler);
    }

    @Bean
    public MessageHandler<Message> messageHandler(EntityHandlerHolder entityHandlerHolder, RowDataHandler<CanalEntry.RowData> rowDataHandler) {
        return  new MessageHandlerImpl(entityHandlerHolder,rowDataHandler);
    }

    @Bean
    public RowDataHandler<CanalEntry.RowData> rowDataHandler() {
        return new RowDataHandlerImpl();
    }

    @Bean
    public EntityHandlerHolder entityHandlerHolder(List<EntityHandler<?>> entityHandlerList) {
        return new EntityHandlerHolder(entityHandlerList);
    }


}
